<?php

declare(strict_types=1);

namespace app\extend\adapter;

use OSS\Core\OssException;
use OSS\OssClient;

/**
 * 阿里云oss
 * composer require aliyuncs/oss-sdk-php
 */
class AliAdapter
{
    public readonly array $config;

    private $client;

    public function __construct($config)
    {
        $this->config = $config;
    }

    private function init() {
        $this->client = new OssClient($this->config['accessId'], $this->config['accessSecret'], $this->config['endpoint']);
    }
    /**
     * 上传
     */
    public function upload(string $path, string $url)
    {
        try{
            $this->init();
            $this->client->uploadFile($this->config['bucket'], $url, $path);
        } catch (OssException $e) {
            tips($e->getMessage());
        }
    }
    /**
     * 下载
     */
    public function download(string $url, string $save_path)
    {
        try{
            $this->init();
            $this->client->downloadFile($this->config['bucket'], $url, $save_path);
        } catch (OssException $e) {
            tips($e->getMessage());
        }
    }
    /**
     * 删除
     */    
    public function delete(string $url)
    {
        try{
            $this->init();
            $this->client->deleteFile($this->config['bucket'], $url);
        } catch (OssException $e) {
            tips($e->getMessage());
        }
    }
}